#include <cassert>
#include <iostream>
#include <cstdio>
#include <GL/glew.h>
#include <sstream>

#include "Hud.hpp"

#include "Player.h"
#include "Asteroid.h"

#include "../../engine/core/FPS.hpp"
#include "../../engine/core/Time.hpp"

Hud::Hud(Player* player, sf::RenderWindow* window) : window(window), player(player) {
    assert(window != NULL);
}

Hud::~Hud() {
    //dtor
}

void Hud::draw() {
    window->pushGLStates();

    glDisable(GL_CULL_FACE);   // needed for text, dont know why
    std::stringstream stream;
    stream << "FPS: " << FPS::getInstance()->getFPS() << std::endl;
    sf::Text fpsText(stream.str());
    fpsText.setCharacterSize(30);
    fpsText.setStyle(sf::Text::Bold);
    fpsText.setColor(sf::Color::White);
    fpsText.setPosition(10, 10);
    window->draw(fpsText);
    stream.str("");  // Clear stream

    stream << "Time played: " << Time::getInstance()->now() << std::endl;
    sf::Text timeText(stream.str());
    timeText.setCharacterSize(30);
    timeText.setStyle(sf::Text::Bold);
    timeText.setColor(sf::Color::White);
    timeText.setPosition(400, 10);
    window->draw(timeText);
    stream.str("");  // Clear stream

    stream << "Player speed: " << Global::getPlayer()->getSpeed()*Global::getPlayer()->getSpeed()/50 <<std::endl;
    sf::Text speed(stream.str());
    speed.setCharacterSize(30);
    speed.setStyle(sf::Text::Bold);
    speed.setColor(sf::Color::White);
    speed.setPosition(400, 90);
    window->draw(speed);
    stream.str("");  // Clear stream

    stream << "Asteroids left: " << Global::getWorld()->getAsteroidsLeft() << std::endl;
    sf::Text asteroidText(stream.str());
    asteroidText.setCharacterSize(30);
    asteroidText.setStyle(sf::Text::Bold);
    asteroidText.setColor(sf::Color::White);
    asteroidText.setPosition(400, 50);
    stream.str("");  // Clear stream

    if(Global::getWorld()->getAsteroidsLeft() == 0) {
        asteroidText.setString("No asteroids left! You have finished the game!");
        asteroidText.setPosition(Global::getWindow()->getSize().x / 6, Global::getWindow()->getSize().y / 2);
    }

    window->draw(asteroidText);

    if(Asteroid* selectedAsteroid = Global::getWorld()->selectedAsteroid) {
        stream << "Asteroid info" << std::endl;
        stream << "Name: " << selectedAsteroid->getName() << std::endl;
        stream << "Location: " << selectedAsteroid->getLocation() << std::endl;
        stream << "Velocity: " << selectedAsteroid->getVelocity() << std::endl;

        sf::Text asteroidText(stream.str());
        asteroidText.setCharacterSize(15);
        asteroidText.setStyle(sf::Text::Bold);
        asteroidText.setColor(sf::Color::White);
        asteroidText.setPosition(10, 50);
        window->draw(asteroidText);
        stream.str("");  // Clear stream
    }

    stream << "Controls: Forward [W], Left [A], Back [S], Right [D], Acceleration [Z], Deceleration [X]" << std::endl;
    stream << "Shoot: Space | Click with mouse on asteroids to get more info" << std::endl;
    sf::Text controlsText(stream.str());
    controlsText.setCharacterSize(15);
    controlsText.setStyle(sf::Text::Bold);
    controlsText.setColor(sf::Color::White);
    controlsText.setPosition(10, Global::getWindow()->getSize().y - 40);
    window->draw(controlsText);
    stream.str("");  // Clear stream

    window->popGLStates();
}
